﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GWB.Application.OnLineShare.Dtos;
using GWB.SqlSugar;
using GWB.SqlSugar.Model;
using Microsoft.AspNetCore.Http;

namespace GWB.Application.OnLineShare
{
    public class OnLineShareService : Repository<ShareInfo>, IOnLineShareService
    {
        private readonly IHttpContextAccessor context;

        public OnLineShareService(IHttpContextAccessor accessor)
        {
            context = accessor;
        }


        public bool AddShareContent(ShareContentInputDto inputDto)
        {
            var ip = context.HttpContext.Connection.RemoteIpAddress.ToString();
            var intranetIp = context.HttpContext.Connection.LocalIpAddress.ToString();
            try
            {
                return base.InsertReturnIdentity(new ShareInfo()
                {
                    Content = inputDto.Content,
                    CreateTime = DateTime.Now,
                    IntranetIP = intranetIp,
                    Type = 1,
                    IP = ip,
                    ShareCode = inputDto.ShareCode
                }) > 0;
            }
            catch (Exception e)
            {
                Console.WriteLine("55555555",e);
            }
            return false;
        }

        public List<ShareContentOutDto> GetShareContent(ShareContentPageInputDto inputDto)
        {
            return new List<ShareContentOutDto>();
            var ip = context.HttpContext.Connection.RemoteIpAddress.ToString();
            var intranetIp = context.HttpContext.Connection.LocalIpAddress.ToString();
            var data = base.Context.Queryable<ShareInfo>()
                .WhereIF(!string.IsNullOrWhiteSpace(inputDto.ShareCode), x => x.ShareCode == inputDto.ShareCode)
                .WhereIF(string.IsNullOrWhiteSpace(inputDto.ShareCode), x => x.IP == ip)
                .Select(it => new ShareContentOutDto
                {
                    Content = it.Content,
                    CreateTime = it.CreateTime.ToString("yyyy-MM-dd HH:mm:ss"),
                    Id = it.Id,
                    Type = it.Type,
                }).ToList();
            foreach (var item in data.Where(item => item.Ip!=null))
            {
                item.Url = $"\\file\\{item.Ip.Replace(":", "_")}\\{item.Content}";
            }
            return data;
        }


        public bool AddFile(IFormFile file)
        {
            var ip = context.HttpContext.Connection.RemoteIpAddress.ToString();
            var intranetIp = context.HttpContext.Connection.LocalIpAddress.ToString();

            
            var filePath = System.IO.Directory.GetCurrentDirectory() +
                           $"\\wwwroot\\file\\{ip.Replace(":", "_")}";
            if (!System.IO.Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }

            Task.Run(() =>
            {
                try
                {
                    using Stream fileStream = new FileStream(filePath + $"\\{file.FileName}", FileMode.Create);
                    file.CopyToAsync(fileStream);
                     base.InsertReturnIdentity(new ShareInfo()
                    {
                        Content = file.FileName,
                        CreateTime = DateTime.Now,
                        IntranetIP = intranetIp,
                        Type =2,
                        IP = ip,
                        ShareCode =""
                    });
                }
                catch (Exception e)
                {
                    Console.WriteLine("文件存储:", e.Message);
                }

            });
            return true;
        }
    }
}
